<template>
  <PageContainer title="文章管理">
    <template #add
      ><el-button @click="publishArticle" type="primary"
        >发布文章</el-button
      ></template
    >
    <!-- 头部表单 -->
    <el-form inline>
      <el-form-item style="width: 2.0504rem" label="文章分类:">
        <ChannelSelect v-model="params.classification"></ChannelSelect>
      </el-form-item>
      <el-form-item style="width: 2.0504rem" label="发布状态:">
        <el-select v-model="params.state" placeholder="请选择">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onsearch">搜索</el-button>
        <el-button @click="onreset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章表格 -->
    <el-table v-loading="loading" :data="tabledata">
      <el-table-column prop="title" label="文章标题">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="classification" label="分类"></el-table-column>
      <el-table-column prop="publishtime" label="发布时间"></el-table-column>
      <el-table-column prop="state" label="状态"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row, $index }">
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="publishArticle(row, $index)"
          ></el-button>
          <el-button
            circle
            plain
            type="primary"
            :icon="Delete"
            @click="deleteitem(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      style="
        justify-content: flex-end;
        margin-top: 0.205rem;
        margin-right: 0.3808rem;
      "
      v-model:currentPage="params.currentPage"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 4, 6, 7]"
      :background="true"
      layout="jumper,total, sizes, prev, pager, next"
      :total="params.total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
    />
    <!-- 添加或编辑文章的抽屉组件 -->
    <ManageArticleEdit
      ref="managearticleedit"
      @success="onsuccess"
    ></ManageArticleEdit>
  </PageContainer>
</template>

<script setup>
import ManageArticleEdit from './components/ManageArticleEdit.vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { reactive, ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import {
  articlegetarticlelistservice,
  articledelservice
} from '@/api/article.js'

const managearticleedit = ref()
let loading = ref(false)
let tabledata = ref([])
const params = reactive({
  classification: '',
  state: '已发布',
  currentPage: 1,
  pagesize: 6,
  total: -1
})
const deleteitem = async (row) => {
  console.log('delete', row)
  await articledelservice(row.articleid)
  getarticlelist()
}
const getarticlelist = async () => {
  console.log('刷新了')
  loading.value = true
  const res = await articlegetarticlelistservice(params)
  loading.value = false
  tabledata.value = reactive(res.data.artlist)
  params.total = res.data.total
}
getarticlelist()
const onSizeChange = (size) => {
  params.currentPage = 1
  params.pagesize = size
  getarticlelist()
}
const onCurrentChange = (currentPage) => {
  params.currentPage = currentPage
  getarticlelist()
}
const onsearch = () => {
  params.currentPage = 1
  getarticlelist()
}
const onreset = () => {
  params.currentPage = 1
  params.classification = ''
  params.state = '已发布'
  params.pagesize = 6
  params.total = -1
  getarticlelist()
}
const publishArticle = (row) => {
  managearticleedit.value.open(row)
}
const onsuccess = async (type) => {
  if (type == 'add') {
    // 添加文章
    const lastpage = Math.ceil((params.total + 1) / params.pagesize)
    params.currentPage = lastpage
  }
  getarticlelist()
}
</script>

<style scoped lang="less">
::v-deep .el-table__row {
  height: 0.4101rem;
  line-height: 0.4101rem;
}
</style>
